import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { UserFollow } from './user-follow.entity';
import { Video } from './video.entity';
import { Favorite } from './favorite.entity';
import { VideoHistory } from './video-history.entity';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    type: 'varchar',
    length: 32,
    unique: true,
    comment: '用户名',
  })
  username: string;

  @Column({
    type: 'varchar',
    length: 100,
    unique: true,
    comment: '邮箱',
  })
  email: string;

  @Column({
    type: 'varchar',
    length: 128,
    comment: '密码',
  })
  password: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '头像URL',
  })
  avatar: string;

  @Column({
    type: 'tinyint',
    width: 1,
    default: 0,
    comment: '性别 0-未知 1-男 2-女',
  })
  gender: number;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '个人简介',
  })
  bio: string;

  @CreateDateColumn({
    type: 'datetime',
    comment: '创建时间',
  })
  created_at: Date;

  @UpdateDateColumn({
    type: 'datetime',
    comment: '更新时间',
  })
  updated_at: Date;

  // @Column({
  //   type: 'bigint',
  //   nullable: true,
  //   comment: '默认收藏夹ID'
  // })
  // default_favorite_id: number;

  @Column({
    type: 'tinyint',
    width: 1,
    default: 0,
    comment: '是否删除 0-否 1-是',
  })
  is_delete: number;

  @Column({ default: 0 })
  following_count: number; // 关注数

  @Column({ default: 0 })
  follower_count: number; // 粉丝数

  // 添加以下两个关系字段
  @OneToMany(() => UserFollow, (userFollow) => userFollow.follower)
  following: UserFollow[]; // 用户主动关注的关系列表

  @OneToMany(() => UserFollow, (userFollow) => userFollow.followed)
  followers: UserFollow[]; // 用户的粉丝列表

  @OneToMany(() => Video, (video) => video.user)
  videos: Video[];

  @OneToMany(() => Favorite, (favorite) => favorite.user)
  favorites: Favorite[];

  @OneToMany(() => VideoHistory, (videoHistory) => videoHistory.user)
  viewHistories: VideoHistory[];
}
